package com.cloudxingkong.dianyinke;

import android.app.Application;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

import org.xutils.x;

import java.io.File;

/**
 * Created by 云星空科技 on 2016/3/16.
 */
public class TeacherApp extends Application {
    private static TeacherApp app;

    @Override
    public void onCreate() {
        super.onCreate();
        app=this;
        x.Ext.init(this);
        initUIL();
        initImageLoad();
    }



    private void initImageLoad() {
        File f= StorageUtils.getOwnCacheDirectory(this,"/image1") ;
        ImageLoaderConfiguration con=new ImageLoaderConfiguration.Builder(this)
                .diskCache(new UnlimitedDiskCache(f)).build();

        ImageLoader.getInstance().init(con);
    }

    private void initUIL() {
        DisplayImageOptions build = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.mipmap.logo_hui_2x)
                .showImageOnFail(R.mipmap.logo_hui_2x)
                .cacheInMemory(true)    //内存缓存
                .cacheOnDisk(true)     //磁盘缓存
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)    //图片压缩格式 二次采样
                .build();

        File cacheDirectory = StorageUtils.getOwnCacheDirectory(this, "DianYinKe/cache/image"); //缓存地址
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .memoryCache(new LruMemoryCache(10 << 20))
                .defaultDisplayImageOptions(build)    //使用
                .memoryCacheSize(10 << 20)     //定义内存缓冲区大小
                .diskCache(new UnlimitedDiskCache(cacheDirectory))   //设置磁盘缓冲区路径
                .diskCacheSize(50 * 1024 * 1024)  //定义磁盘缓冲区大小
                .imageDownloader(new BaseImageDownloader(this, 20 * 60 * 60, 30 * 60 * 60))
                .diskCacheFileCount(100)    //设置缓存文件数量
                .denyCacheImageMultipleSizesInMemory()
                .writeDebugLogs()
                .build();
        ImageLoader.getInstance().init(config);

    }

    public static TeacherApp getInstance(){
        return app;
    }

}
